package com.openbravo.data.model;

import com.openbravo.basic.BasicException;
import com.openbravo.data.gui.ListCellRendererBasic;
import com.openbravo.data.loader.ComparatorCreator;
import com.openbravo.data.loader.DataRead;
import com.openbravo.data.loader.DataWrite;
import com.openbravo.data.loader.Datas;
import com.openbravo.data.loader.IRenderString;
import com.openbravo.data.loader.PreparedSentence;
import com.openbravo.data.loader.SentenceExec;
import com.openbravo.data.loader.SentenceList;
import com.openbravo.data.loader.SerializerRead;
import com.openbravo.data.loader.SerializerWrite;
import com.openbravo.data.loader.Session;
import com.openbravo.data.loader.Vectorer;
import com.openbravo.data.user.FilterEditorCreator;
import com.openbravo.data.user.ListProvider;
import com.openbravo.data.user.ListProviderCreator;
import com.openbravo.data.user.SaveProvider;
import com.openbravo.format.Formats;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;

import javax.swing.ListCellRenderer;

public class Row
{
  private Field[] fields;

  public Row(Field[] paramArrayOfField)
  {
    this.fields = paramArrayOfField;
  }

  public Vectorer getVectorer()
  {
    return new RowVectorer( );
  }

  public IRenderString getRenderString()
  {
    return new RowRenderString( );
  }

  public ListCellRenderer getListCellRenderer()
  {
    return new ListCellRendererBasic(new RowRenderString( ));
  }

  public ComparatorCreator getComparatorCreator()
  {
    return new RowComparatorCreator();
  }

  public SentenceExec getExecSentence(Session paramSession, String paramString, final int[] indexes)
  {
    return new PreparedSentence(paramSession, paramString, new SerializerWrite<Object[]>( )
    {
      public void writeValues(DataWrite paramDataWrite, Object[] paramArrayOfObject)
        throws BasicException
      {
        for (int i = 0; i < indexes.length; i++)
          Row.this.fields[ indexes[i]].getData().setValue(paramDataWrite, i + 1, paramArrayOfObject[ indexes[i]]);
      }
 
    });
  }

  public ListProvider getListProvider(Session paramSession, Table paramTable)
  {
    return new ListProviderCreator(getListSentence(paramSession, paramTable));
  }

  public SaveProvider getSaveProvider(Session paramSession, Table paramTable)
  {
    return new SaveProvider(getUpdateSentence(paramSession, paramTable), getInsertSentence(paramSession, paramTable), getDeleteSentence(paramSession, paramTable));
  }

  public SentenceList getListSentence(Session paramSession, String paramString, SerializerWrite paramSerializerWrite)
  {
    return new PreparedSentence(paramSession, paramString, paramSerializerWrite, new RowSerializerRead( ));
  }

  public ListProvider getListProvider(Session paramSession, String paramString, FilterEditorCreator paramFilterEditorCreator)
  {
    return new ListProviderCreator(getListSentence(paramSession, paramString, paramFilterEditorCreator.getSerializerWrite()), paramFilterEditorCreator);
  }

  public SentenceList getListSentence(Session paramSession, Table paramTable)
  {
    return getListSentence(paramSession, paramTable.getListSQL(), null);
  }

  public SentenceExec getInsertSentence(Session paramSession, final Table paramTable)
  {
    return new PreparedSentence(paramSession, paramTable.getInsertSQL(), new SerializerWrite< Object[]>( )
    {
      public void writeValues(DataWrite paramDataWrite, Object[] paramArrayOfObject)
        throws BasicException
      {
    	  Table val$t=paramTable;
        for (int i = 0; i < val$t.getColumns().length; i++)
          Row.this.fields[i].getData().setValue(paramDataWrite, i + 1, paramArrayOfObject[i]);
      }
    });
  }

  public SentenceExec getDeleteSentence(Session paramSession, final Table paramTable)
  {
    return new PreparedSentence(paramSession, paramTable.getDeleteSQL(), new SerializerWrite< Object[] >( )
    {
      public void writeValues(DataWrite paramDataWrite, Object[] paramArrayOfObject)
        throws BasicException
      {
    	  Table val$t=paramTable;
        int i = 1;
        for (int j = 0; j < val$t.getColumns().length; j++)
        {
          if (!val$t.getColumns()[j].isPK())
            continue;
          Row.this.fields[j].getData().setValue(paramDataWrite, i++, paramArrayOfObject[j]);
        }
      }
    });
  }

  public SentenceExec getUpdateSentence(Session paramSession, final Table paramTable)
  {
    return new PreparedSentence(paramSession, paramTable.getUpdateSQL(), new SerializerWrite< Object[]>( )
    {
      public void writeValues(DataWrite paramDataWrite, Object[] paramArrayOfObject)
        throws BasicException
      {
    	  Table val$t=paramTable;
        int i = 1;
        for (int j = 0; j < val$t.getColumns().length; j++)
        {
          if (val$t.getColumns()[j].isPK())
            continue;
          Row.this.fields[j].getData().setValue(paramDataWrite, i++, paramArrayOfObject[j]);
        }
        for (int j = 0; j < val$t.getColumns().length; j++)
        {
          if (!val$t.getColumns()[j].isPK())
            continue;
          Row.this.fields[j].getData().setValue(paramDataWrite, i++, paramArrayOfObject[j]);
        }
      }
    });
  }

  public Datas[] getDatas()
  {
    Datas[] arrayOfDatas = new Datas[this.fields.length];
    for (int i = 0; i < this.fields.length; i++)
      arrayOfDatas[i] = this.fields[i].getData();
    return arrayOfDatas;
  }

  public SerializerRead getSerializerRead()
  {
    return new RowSerializerRead( );
  }

  private class RowComparatorCreator
    implements ComparatorCreator
  {
    private List<Integer> comparablefields = new ArrayList();

    public RowComparatorCreator()
    {
      for (int i = 0; i < Row.this.fields.length; i++)
      {
        if (!Row.this.fields[i].isComparable())
          continue;
        this.comparablefields.add(Integer.valueOf(i));
      }
    }

    public String[] getHeaders()
    {
      String[] arrayOfString = new String[this.comparablefields.size()];
      for (int i = 0; i < this.comparablefields.size(); i++) {
		int intValue = ((Integer)this.comparablefields.get(i)).intValue();
		//Row.access$200(Row.this)
		arrayOfString[i] = fields[intValue].getLabel();
	}
      return arrayOfString;
    }

    public Comparator createComparator(final int[] paramArrayOfInt)
    {
    //  Object o=paramArrayOfInt;
      return new Comparator ( )
      {
        public int compare(Object paramObject1, Object paramObject2)
        {
          if (paramObject1 == null)
          {
            if (paramObject2 == null)
              return 0;
            return -1;
          }
          if (paramObject2 == null)
            return 1;
          Object[] arrayOfObject1 = (Object[])(Object[])paramObject1;
          Object[] arrayOfObject2 = (Object[])(Object[])paramObject2;
          int[]   val$orderby= paramArrayOfInt;
          for (int i = 0; i <  val$orderby.length; i++)
          {
        	
            int j = Row.this.fields[((Integer)Row.RowComparatorCreator.this.comparablefields.get(val$orderby[i])).intValue()].getData().compare(arrayOfObject1[((Integer)Row.RowComparatorCreator.this.comparablefields.get(val$orderby[i])).intValue()], arrayOfObject2[((Integer)Row.RowComparatorCreator.this.comparablefields.get(val$orderby[i])).intValue()]);
            if (j != 0)
              return j;
          }
          return 0;
        }
      };
    }
  }

  private class RowRenderString
    implements IRenderString
  {
    private RowRenderString()
    {
    }

    public String getRenderString(Object paramObject)
    {
      Object[] arrayOfObject = (Object[])(Object[])paramObject;
      StringBuffer localStringBuffer = new StringBuffer();
      for (int i = 0; i < Row.this.fields.length; i++)
      {
        if (!Row.this.fields[i].isTitle())
          continue;
        if (localStringBuffer.length() > 0)
          localStringBuffer.append(" - ");
        localStringBuffer.append(Row.this.fields[i].getFormat().formatValue(arrayOfObject[i]));
      }
      return localStringBuffer.toString();
    }
  }

  private class RowVectorer
    implements Vectorer
  {
    private RowVectorer()
    {
    }

    public String[] getHeaders()
      throws BasicException
    {
      ArrayList localArrayList = new ArrayList();
      for (Field localField : Row.this.fields)
      {
        if (!localField.isSearchable())
          continue;
        localArrayList.add(localField.getLabel());
      }
      return (String[])localArrayList.toArray(new String[localArrayList.size()]);
    }

    public String[] getValues(Object paramObject)
      throws BasicException
    {
      Object[] arrayOfObject = (Object[])(Object[])paramObject;
      ArrayList localArrayList = new ArrayList();
      for (int i = 0; i < Row.this.fields.length; i++)
      {
        if (!Row.this.fields[i].isSearchable())
          continue;
        localArrayList.add(Row.this.fields[i].getFormat().formatValue(arrayOfObject[i]));
      }
      return (String[])localArrayList.toArray(new String[localArrayList.size()]);
    }
  }

  private class RowSerializerRead
    implements SerializerRead
  {
    private RowSerializerRead()
    {
    }

    public Object readValues(DataRead paramDataRead)
      throws BasicException
    {
      Object[] arrayOfObject = new Object[Row.this.fields.length];
      for (int i = 0; i < Row.this.fields.length; i++)
        arrayOfObject[i] = fields[i].getData().getValue(paramDataRead, i + 1);
      return arrayOfObject;
    }
  }
}

/* Location:           D:\PowerFulPOS-2.30.2\openbravopos.jar
 * Qualified Name:     com.openbravo.data.model.Row
 * JD-Core Version:    0.6.0
 */